﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace MDIApplication
{
    public partial class CheckOut : Form
    {
        public CheckOut()
        {
            InitializeComponent();           
        }

        #region Form_Load
        private void CheckOut_Load(object sender, EventArgs e)
        {
            int yearSpan = DateTime.Now.Year + 2;
            dateTimePicker1.MinDate = new DateTime(yearSpan, 1, 1);
            textBox2.MaxLength = 4;
            textBox4.MaxLength = 4;
            textBox5.MaxLength = 4;
            textBox6.MaxLength = 4;

            comboBox3.SelectedIndex = 0;
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;

            FillCombox1();
        }
        #endregion Form_Load

        #region FillCombox

        void FillCombox1()
        {
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            string query = "Select g.Guest_ID From Guest g, Booking bk, Room rm where bk.Guest_ID=g.Guest_ID and bk.Room_Unit_No=rm.Room_Unit_No and rm.Room_Status='Occupied'";
            SqlCommand cmdDatabase = new SqlCommand(query, connection);
            SqlDataReader dr;
            try
            {
                connection.Open();
                dr = cmdDatabase.ExecuteReader();
                while (dr.Read())
                {
                   String guestId = (string)dr["Guest_ID"];
                   comboBox1.Items.Add(guestId);               
                }
                if (comboBox1.Items.Count == 0)
                {
                    MessageBox.Show("There is no booking need to be checked out");
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }            
        }

        void FillCombox2(string comboBox1Text)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            string query = "Select bk.Booking_ID From Booking bk, Room rm where bk.Room_Unit_No=rm.Room_Unit_No and rm.Room_Status='Occupied' and bk.Guest_ID='" + comboBox1Text+ "'";
            SqlCommand cmdDatabase = new SqlCommand(query, connection);
            SqlDataReader dr;
            try
            {
                connection.Open();
                dr = cmdDatabase.ExecuteReader();
                while (dr.Read())
                {
                    String bookingId = (string)dr["Booking_ID"];
                    comboBox2.Items.Clear();
                    comboBox2.Items.Add(bookingId);
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion FillCombox

        #region toolStrip
        NewBooking newBookingChild;
        AllBookingscs allBookingChild;
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                allBookingChild.MdiParent = this.MdiParent;
                // Display the form.
                allBookingChild.Show();
                allBookingChild.Activate();
                if (newBookingChild == null)
                {
                    newBookingChild = new NewBooking();
                    newBookingChild.MdiParent = this.MdiParent;
                    newBookingChild.Location = new Point(100, 70);
                    // Display the form.
                    newBookingChild.Show();
                    newBookingChild.Activate();
                }
            }
        }

        CheckIn checkInChild;
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkInChild == null)
            {
                checkInChild = new CheckIn();
                checkInChild.MdiParent = this.MdiParent;
                checkInChild.Location = new Point(0, 0);
                checkInChild.Show();
                checkInChild.Activate();
            }
        }

        CheckOut checkOutChild;
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (checkOutChild == null)
            {
                checkOutChild = new CheckOut();
                checkOutChild.MdiParent = this.MdiParent;
                checkOutChild.Location = new Point(0, 0);
                checkOutChild.Show();
                checkOutChild.Activate();
            }
        }

        GuestManagement guestManagementChild;
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (guestManagementChild == null)
            {
                guestManagementChild = new GuestManagement();
                guestManagementChild.Location = new Point(0, 0);
                guestManagementChild.MdiParent = this.MdiParent;
                guestManagementChild.Show();
                guestManagementChild.Activate();
            }
        }
        #endregion toolStrip

        #region comboBox SelectedIndexChanged
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {          
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            string query = "Select * From Guest where Guest_ID = '" + comboBox1.Text + "';";
            SqlCommand cmdDatabase = new SqlCommand(query, connection);
            SqlDataReader dr;
            try
            {
                connection.Open();
                dr = cmdDatabase.ExecuteReader();
                while (dr.Read())
                {
                    lblFirstName.Text= (string)dr["First_Name"];
                    lblLastName.Text = (string)dr["Last_Name"];
                    lblICNo.Text = (string)dr["IC_No"];
                    lblContactNo.Text = Convert.ToString(dr["Contact_No"]);
                    lblAddress.Text = (string)dr["Address"];
                    lblCheckInDate.Text = "";
                    lblCheckOutDate.Text ="";
                    lblNightofStay.Text = "";
                    lblRoomRate.Text = "";
                    if (textBox1.Text.Trim().Length == 0)
                    {
                        lblTotalPayment.Text = "";
                    }
                    else
                    {
                        lblTotalPayment.Text = Convert.ToString(textBox1.Text);
                    }    
                                       
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            FillCombox2(comboBox1.Text);
        }

        double totalPayment;
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            string query = "Select * From Booking where Booking_ID = '" + comboBox2.Text + "';";
            SqlCommand cmdDatabase = new SqlCommand(query, connection);
            SqlDataReader dr;
            try
            {
                connection.Open();
                dr = cmdDatabase.ExecuteReader();
                while (dr.Read())
                {
                    lblCheckInDate.Text=Convert.ToDateTime(dr["Check_in_Date"]).ToShortDateString();
                    lblCheckOutDate.Text = Convert.ToDateTime(dr["Check_out_Date"]).ToShortDateString();
                    lblNightofStay.Text = Convert.ToString(dr["Nights_of_Stay"]);
                    int roomUnitNo = Convert.ToInt32(dr["Room_Unit_No"]);
                    BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();
                    Room r = db.getRoomByRoomUnitNo(roomUnitNo);
                    lblRoomRate.Text = r.roomRate.ToString();
                    if (textBox1.Text.Trim().Length == 0)
                    {
                        totalPayment = Convert.ToDouble(lblRoomRate.Text) * Convert.ToDouble(lblNightofStay.Text);
                        lblTotalPayment.Text = Convert.ToString(totalPayment);
                    }
                    else
                    {
                        totalPayment = Convert.ToDouble(textBox1.Text) + Convert.ToDouble(lblRoomRate.Text) * Convert.ToDouble(lblNightofStay.Text);
                        lblTotalPayment.Text = Convert.ToString(totalPayment);
                    }              
                    lblTotalPayment.Text = Convert.ToString(totalPayment);
                }
                connection.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion comboBox SelectedIndexChanged

        #region textBox

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            validateTextInteger(textBox1);
            if (textBox1.Text.Trim().Length == 0)
            {
                totalPayment = Convert.ToDouble(lblRoomRate.Text) * Convert.ToDouble(lblNightofStay.Text);
                lblTotalPayment.Text = Convert.ToString(totalPayment);
            }
            else
            {
                totalPayment = Convert.ToDouble(textBox1.Text) + Convert.ToDouble(lblRoomRate.Text) * Convert.ToDouble(lblNightofStay.Text);
                lblTotalPayment.Text = Convert.ToString(totalPayment);              
            }
        }

        private void validateTextInteger(TextBox textBox)
        {
            Exception X = new Exception();

            TextBox T = textBox;

            try
            {
                if (T.Text != "-")
                {
                    int x = int.Parse(T.Text);
                }
            }
            catch (Exception)
            {
                try
                {
                    int CursorIndex = T.SelectionStart - 1;
                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index
                    T.SelectionStart = CursorIndex;
                    T.SelectionLength = 0;
                }
                catch (Exception) { }
            }
        }


        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            validateTextInteger(textBox2);  
            int i;
            if (textBox2.Text.Length == 4 && int.TryParse(textBox2.Text, out i))
            {
                textBox4.Focus();
            }

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            validateTextInteger(textBox4);  
            int i;
            if (textBox4.Text.Length == 4 && int.TryParse(textBox4.Text, out i))
            {
                textBox5.Focus();
            }
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            validateTextInteger(textBox5);  
            int i;
            if (textBox5.Text.Length == 4 && int.TryParse(textBox5.Text, out i))
            {
                textBox6.Focus();
            }
        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {
            validateTextInteger(textBox6);  
            int i;
            if (textBox6.Text.Length == 4 && int.TryParse(textBox6.Text, out i))
            {
                comboBox3.Focus();
            }
        }
        #endregion textBox

        #region GenereteInvoiceButton

        BaseFont f_cb = BaseFont.CreateFont("c:\\windows\\fonts\\calibrib.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.Text == String.Empty || comboBox2.Text == String.Empty || textBox3.Text == String.Empty || textBox4.Text == String.Empty || textBox5.Text == String.Empty)
            {
                MessageBox.Show("Pleasae fill up all required information."); 
            }
            else
            {
                //Insert Invoice and Credit Card
                BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();
                List<Invoice> invoiceList = db.getAllInvoice();
                Invoice iLast = invoiceList.Last();
                Invoice i = new Invoice();
                i.invoiceId = NxtKeyCode(iLast.invoiceId);
                i.invoiceDate = DateTime.Now;
                i.guestId = comboBox1.Text;
                i.bookingId = comboBox2.Text;
                i.roomExpense = Convert.ToDouble(lblRoomRate.Text) * Convert.ToDouble(lblNightofStay.Text);
                i.creditCardNo = textBox2.Text + textBox4.Text + textBox5.Text + textBox6.Text;
                if (textBox1.Text.Trim().Length == 0)
                {
                    i.additionalCost = 0.00;
                }
                else
                {
                    i.additionalCost = Convert.ToDouble(textBox1.Text);
                }

                i.totalPayment = Convert.ToDouble(lblTotalPayment.Text);
                CreditCard cc = new CreditCard();
                cc.creditCardNo = i.creditCardNo;
                cc.cardType = comboBox3.Text;
                cc.cardHolderName = textBox3.Text;
                cc.expiryDate = dateTimePicker1.Value;

                //insert
                if (db.insertCreditCard(cc)==true)
                {
                    db.insertInvoice(i);
                }             

                //update room
                Booking bk = db.getBookingByBookingID(comboBox2.Text);
                db.updateRoomStatus(bk.roomUnitNo, "Cleaning");

                //Create PDF
                string fileLocation = "C:\\Users\\dell\\Desktop\\" + i.invoiceId + ".pdf";
                Document document = new Document(PageSize.A4, 25, 25, 30, 1);
                PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(fileLocation, FileMode.Create));

                document.Open();

                // Add meta information to the document
                document.AddCreator("Delonix Regia Hotel Reception");
                document.AddKeywords("Invoice");
                document.AddSubject("Invoice for Guest");
                document.AddTitle("Delonix Regia Hotel Invoice");

                // Open the document to enable you to write to the document
                document.Open();

                // Makes it possible to add text to a specific place in the document using 
                // a X & Y placement syntax.
                PdfContentByte cb = writer.DirectContent;
                // Add a footer template to the document
                cb.AddTemplate(PdfFooter(cb), 30, 1);

                // Add a logo to the invoice
                iTextSharp.text.Image png = iTextSharp.text.Image.GetInstance(@"C:\Users\dell\Documents\Visual Studio 2012\Projects\MDIApplication(14)\MDIApplication\Resources\Logo.PNG");
                png.ScaleAbsolute(250, 155);
                png.SetAbsolutePosition(40, 750);
                cb.AddImage(png);

                // First we must activate writing
                cb.BeginText();
                // First we write out the header information

                // Start with the invoice type header
                writeText(cb, "Delonix Regia Hotel Invoice", 350, 820, f_cb, 14);
                // HEader details; invoice number, invoice date, due date and customer Id
                writeText(cb, "Invoice No", 350, 800, f_cb, 10);
                writeText(cb, i.invoiceId, 420, 800, f_cn, 10);
                writeText(cb, "Invoice date", 350, 788, f_cb, 10);
                writeText(cb, i.invoiceDate.ToShortDateString(), 420, 788, f_cn, 10);
                writeText(cb, "Customer Name", 350, 776, f_cb, 10);
                writeText(cb, lblFirstName.Text + " " + lblLastName.Text, 420, 776, f_cn, 10);


                // Write out invoice terms details
                int left_margin = 40;
                int top_margin = 620;
                writeText(cb, "Check in Date", left_margin, top_margin, f_cb, 10);
                writeText(cb, lblCheckInDate.Text, left_margin, top_margin - 12, f_cn, 10);
                writeText(cb, "Check out Date", left_margin + 200, top_margin, f_cb, 10);
                writeText(cb, lblCheckOutDate.Text, left_margin + 200, top_margin - 12, f_cn, 10);
                writeText(cb, "Payment terms", left_margin + 350, top_margin, f_cb, 10);
                writeText(cb, "Credit Card", left_margin + 350, top_margin - 12, f_cn, 10);
                // Move down
                left_margin = 40;
                top_margin = 590;
                writeText(cb, "Credit Card No", left_margin, top_margin, f_cb, 10);
                writeText(cb, textBox2.Text, left_margin, top_margin - 12, f_cn, 10);
                writeText(cb, "Credit Card Type", left_margin + 200, top_margin, f_cb, 10);
                writeText(cb, comboBox3.Text, left_margin + 200, top_margin - 12, f_cn, 10);
                writeText(cb, "Card Holder Name", left_margin + 350, top_margin, f_cb, 10);
                writeText(cb, textBox3.Text, left_margin + 350, top_margin - 12, f_cn, 10);

                // NOTE! You need to call the EndText() method before we can write graphics to the document!
                cb.EndText();
                // Separate the header from the rows with a line
                // Draw a line by setting the line width and position
                cb.SetLineWidth(0f);
                cb.MoveTo(40, 570);
                cb.LineTo(560, 570);
                cb.Stroke();
                // Don't forget to call the BeginText() method when done doing graphics!
                cb.BeginText();

                // Before we write the lines, it's good to assign a "last position to write"
                // variable to validate against if we need to make a page break while outputting.
                // Change it to 510 to write to test a page break; the fourth line on a new page
                int lastwriteposition = 100;

                // Loop thru the rows in the rows table
                // Start by writing out the line headers
                top_margin = 550;
                left_margin = 40;
                // Line headers
                writeText(cb, "No of Adults", left_margin, top_margin, f_cb, 10);
                writeText(cb, "No of Children", left_margin + 100, top_margin, f_cb, 10);
                writeText(cb, "Room Unit No", left_margin + 220, top_margin, f_cb, 10);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, "Nights of Stay", left_margin + 400, top_margin, 0);
                writeText(cb, "Room Rate", left_margin + 450, top_margin, f_cb, 10);

                // First item line position starts here
                top_margin = 538;

                // Loop thru the table of items and set the linespacing to 12 points.
                // Note that we use the -= operator, the coordinates goes from the bottom of the page!

                writeText(cb, Convert.ToString(bk.numberOfAdults), left_margin, top_margin, f_cn, 10);
                writeText(cb, Convert.ToString(bk.numberOfChildren), left_margin + 100, top_margin, f_cn, 10);
                writeText(cb, Convert.ToString(bk.roomUnitNo), left_margin + 220, top_margin, f_cn, 10);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, Convert.ToString(bk.nightsOfStay), left_margin + 400, top_margin, 0);
                writeText(cb, lblRoomRate.Text, left_margin + 450, top_margin, f_cn, 10);

                // This is the line spacing, if you change the font size, you might want to change this as well.
                top_margin -= 12;

                // Implement a page break function, checking if the write position has reached the lastwriteposition
                if (top_margin <= lastwriteposition)
                {
                    // We need to end the writing before we change the page
                    cb.EndText();
                    // Make the page break
                    document.NewPage();
                    // Start the writing again
                    cb.BeginText();
                    // Assign the new write location on page two!
                    // Here you might want to implement a new header function for the new page
                    top_margin = 780;
                }


                // Okay, write out the totals table
                // Here you might want to do some page break scenarios, as well:
                // Example:
                // Calculate how many rows you are about to print and see if they fit before the lastwriteposition, 
                // then decide how to do; write some on first page, then the rest on second page or perhaps all the 
                // total lines after the page break.
                // We are not doing this here, we just write them out 80 points below the last writed item row

                top_margin -= 80;
                left_margin = 350;

                // First the headers
                writeText(cb, "Room Expenses", left_margin, top_margin, f_cb, 10);
                writeText(cb, "Additional Cost", left_margin, top_margin - 12, f_cb, 10);
                writeText(cb, "Total Payment", left_margin, top_margin - 48, f_cb, 10);
                // Move right to write out the values
                left_margin = 540;
                // Write out the invoice currency and values in regular text
                cb.SetFontAndSize(f_cn, 10);
                //string curr = drTotal["currency"].ToString();
                //cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, curr, left_margin, top_margin, 0);
                //cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, curr, left_margin, top_margin - 12, 0);
                //cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, curr, left_margin, top_margin - 24, 0);
                //cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, curr, left_margin, top_margin - 48, 0);
                left_margin = 535;
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, Convert.ToString(i.roomExpense), left_margin, top_margin, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, textBox1.Text, left_margin, top_margin - 12, 0);
                cb.ShowTextAligned(PdfContentByte.ALIGN_RIGHT, Convert.ToString(totalPayment), left_margin, top_margin - 48, 0);



                // End the writing of text
                cb.EndText();

                // Close the document, the writer and the filestream!
                document.Close();
                writer.Close();

                //Open PDF file
                System.Diagnostics.Process.Start(fileLocation);

                //transfer
                CloseAllForms();
                if (allBookingChild == null)
                {
                    allBookingChild = new AllBookingscs();
                    allBookingChild.Location = new Point(0, 0);
                    // Set the Parent Form of the Child window.
                    allBookingChild.MdiParent = this.MdiParent;
                    // Display the form.
                    allBookingChild.Show();
                    allBookingChild.Activate();         
                }
            }
        }
        #endregion GenereteInvoiceButton

        #region backButton
        private void button2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.Location = new Point(0, 0);
                // Set the Parent Form of the Child window.
                allBookingChild.MdiParent = this.MdiParent;
                // Display the form.
                allBookingChild.Show();
                allBookingChild.Activate();            
            }
        }
        #endregion backButton

        #region PDF
        private void writeText(PdfContentByte cb, string Text, int X, int Y, BaseFont font, int Size)
        {
            cb.SetFontAndSize(font, Size);
            cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, Text, X, Y, 0);
        }

        private PdfTemplate PdfFooter(PdfContentByte cb)
        {
            // Create the template and assign height
            PdfTemplate tmpFooter = cb.CreateTemplate(580, 70);
            // Move to the bottom left corner of the template
            tmpFooter.MoveTo(1, 1);
            // Place the footer content
            tmpFooter.Stroke();
            // Begin writing the footer
            tmpFooter.BeginText();
            // Set the font and size
            tmpFooter.SetFontAndSize(f_cn, 8);
            // Write out details from the payee table
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Delonix Regia Hotel", 0, 53, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Address: Tampines Ave 4", 0, 45, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Zip: 520816 Singapore", 0, 37, 0);

            // Bold text for ther headers
            tmpFooter.SetFontAndSize(f_cb, 8);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Phone", 215, 53, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "E-Mail", 215, 45, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Webite", 215, 37, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "Fax", 400, 53, 0);
            // Regular text for infomation fields
            tmpFooter.SetFontAndSize(f_cn, 8);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "81867013", 265, 53, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "luckypx615@gmail.com", 265, 45, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "www.tp.edu.sg", 265, 37, 0);
            tmpFooter.ShowTextAligned(PdfContentByte.ALIGN_LEFT, "+65 65587091", 450, 53, 0);
            // End text
            tmpFooter.EndText();
            // Stamp a line above the page footer
            cb.SetLineWidth(0f);
            cb.MoveTo(30, 60);
            cb.LineTo(570, 60);
            cb.Stroke();
            // Return the footer template
            return tmpFooter;
        }
        #endregion PDF

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                checkInChild = null;
                allBookingChild = null;
                newBookingChild = null;
                guestManagementChild = null;
            }
        }
        #endregion CloseAllForms

        #region AlphanumericIncrementForID
        public string NxtKeyCode(string KeyCode)
        {
            byte[] ASCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode);
            int StringLength = ASCIIValues.Length;
            bool isAllZed = true;
            bool isAllNine = true;
            //Check if all has ZZZ.... then do nothing just return empty string.

            for (int i = 0; i < StringLength - 1; i++)
            {
                if (ASCIIValues[i] != 90)
                {
                    isAllZed = false;
                    break;
                }
            }
            if (isAllZed && ASCIIValues[StringLength - 1] == 57)
            {
                ASCIIValues[StringLength - 1] = 64;
            }

            // Check if all has 999... then make it A0
            for (int i = 0; i < StringLength; i++)
            {
                if (ASCIIValues[i] != 57)
                {
                    isAllNine = false;
                    break;
                }
            }
            if (isAllNine)
            {
                ASCIIValues[StringLength - 1] = 47;
                ASCIIValues[0] = 65;
                for (int i = 1; i < StringLength - 1; i++)
                {
                    ASCIIValues[i] = 48;
                }
            }


            for (int i = StringLength; i > 0; i--)
            {
                if (i - StringLength == 0)
                {
                    ASCIIValues[i - 1] += 1;
                }
                if (ASCIIValues[i - 1] == 58)
                {
                    ASCIIValues[i - 1] = 48;
                    if (i - 2 == -1)
                    {
                        break;
                    }
                    ASCIIValues[i - 2] += 1;
                }
                else if (ASCIIValues[i - 1] == 91)
                {
                    ASCIIValues[i - 1] = 65;
                    if (i - 2 == -1)
                    {
                        break;
                    }
                    ASCIIValues[i - 2] += 1;

                }
                else
                {
                    break;
                }

            }
            KeyCode = ASCIIEncoding.ASCII.GetString(ASCIIValues);
            return KeyCode;
        }
        #endregion AlphanumericIncrementForID


    }
}
